package com.gogowan.agroenterprise.supply;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.CollectionSDInfo;
import com.gogowan.agroenterprise.model.CommodityRequst;
import com.gogowan.agroenterprise.model.EnterpriseDetailInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.MoreDetailResultInfo;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.model.SupplyDemandDescInfo;
import com.gogowan.agroenterprise.supply.utils.SupplyAndDemandUtils;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * 详细内容页
 * @author Maniac
 *	2012.12.18
 */
public class MaterialDetailActivity extends BaseActivity{

	private TextView title,introduction,content,enterpriseDescription;
	private Button check,collect;
	private SupplyDemandDescInfo supplyDemandDescInfo;
	
	private SharedPreferences preferences;
	private long enterpriseId;
	private ProgressDialog progressDialog;
	private int sdType;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_material_detail);
		
		title = (TextView)findViewById(R.id.material_detail_title);
		introduction = (TextView)findViewById(R.id.material_detail_introduction);
		//date = (TextView)findViewById(R.id.material_detail_date);
		content = (TextView)findViewById(R.id.material_detail_content);
		enterpriseDescription  = (TextView)findViewById(R.id.material_detail_enterprise_description);
		check = (Button)findViewById(R.id.material_detail_btn_check);
		collect = (Button)findViewById(R.id.material_detail_btn_collect);
		
		preferences = getSharedPreferences("LoginINfo", MODE_PRIVATE);
		enterpriseId = preferences.getLong("enterpriseid", 0);
		
		Intent intent = getIntent();
		String string = intent.getStringExtra("MaterialDetail");
		sdType = intent.getIntExtra("sdType", 0);
		
		if (sdType == 1) {
			check.setEnabled(false);
		}
		
		if (string == null || string.equals("")) {
			showFinishDialog("提示","初始化数据失败！");
		} else {
			MoreDetailResultInfo materialResultInfo = (MoreDetailResultInfo)Utils.fromJSON(string, MoreDetailResultInfo.class);
			supplyDemandDescInfo = materialResultInfo.getSupplyDemandInfo();
			if (supplyDemandDescInfo == null) {
				showFinishDialog("提示","初始化数据失败！");
			} else {
				
				String titleString =  supplyDemandDescInfo.getTitle();
				String introdutctionString = supplyDemandDescInfo.getIntroduction();
				//String dateString = supplyDemandDescInfo.getTimeDown();
				String contentString = supplyDemandDescInfo.getDescription();
				
				if (titleString == null || titleString.equals("")) {
					showFinishDialog("提示","异常数据(标题null)！");
				} else {
					title.setText(SupplyAndDemandUtils.ToDBC(titleString));
					
					if (introdutctionString == null || introdutctionString.equals("")) {
						showFinishDialog("提示","异常数据(简介null)！");
					} else {
						introduction.setText(SupplyAndDemandUtils.ToDBC(introdutctionString));
						
						/*if (dateString == null || dateString.equals("")) {
							showFinishDialog("提示","异常数据(产品过期时间null)！");
						} else {
							date.setText(dateString);
							*/
							if (contentString == null || contentString.equals("")) {
								showFinishDialog("提示","异常数据(详细介绍null)！");
							} else {
								content.setText(SupplyAndDemandUtils.ToDBC(contentString));
								
								EnterpriseDetailInfo enterpriseInfo = materialResultInfo.getEnterpriseInfo();
								if (enterpriseInfo != null) {
									String enterpriseString = enterpriseInfo.getEnterpriseDescription();
									if (enterpriseString == null || enterpriseString.equals("")) {
										enterpriseDescription.setText("未获取到企业信息");
									} else {
										enterpriseDescription.setText(SupplyAndDemandUtils.ToDBC(enterpriseString));
									}
								} else {
									enterpriseDescription.setText("未获取到企业信息");
								}
							}
						
					}
				}
			}
			}
		
		
		check.setOnClickListener(checkCallback);
		collect.setOnClickListener(collectCallback);
	}

	/**
	 * 查看详细信息点击事件
	 */
	public Button.OnClickListener checkCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			
			CommodityRequst commodityRequst = new CommodityRequst();
			commodityRequst.setCommodityID(supplyDemandDescInfo.getMaterialID());
			
			progressDialog = ProgressDialog.show(MaterialDetailActivity.this,
					getString(R.string.alert_title),
					getString(R.string.alert_message), true, false);
			
			GetCommodityInfoAsyn getCommodityInfoAsyn = new GetCommodityInfoAsyn(MaterialDetailActivity.this, progressDialog);
			getCommodityInfoAsyn.execute(commodityRequst);
		}
		
	};
	
	/**
	 * 收藏按钮点击事件
	 */
	public Button.OnClickListener collectCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			
			CollectionSDInfo collectionSDInfo = new CollectionSDInfo();
			collectionSDInfo.setEnterpriseID(enterpriseId);
			collectionSDInfo.setFromEnterpriseID(supplyDemandDescInfo.getEnterpriseID());
			collectionSDInfo.setSupplyDemandID(supplyDemandDescInfo.getSdID());
			collectionSDInfo.setTypeCode(sdType);
			progressDialog = ProgressDialog.show(MaterialDetailActivity.this,
					getString(R.string.alert_title),
					getString(R.string.alert_message), true, false);
			
			CollectAsyn collectAsyn = new CollectAsyn();
			collectAsyn.execute(collectionSDInfo);
		}
		
	};
	
	private void showFinishDialog(String s,String string){
    	new AlertDialog.Builder(MaterialDetailActivity.this).setTitle(s)
		.setMessage(string).setPositiveButton("确定", new DialogInterface.OnClickListener() {
	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				MaterialDetailActivity.this.finish();
			}
		}).create().show();
    }
	
	/**
	 * 收藏异步
	 * @author Maniac
	 * 2012.12.18
	 */
	private class CollectAsyn extends MyAsyncTask{

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(MaterialDetailActivity.this, getString(R.string.server_error), Toast.LENGTH_LONG).show();
			} else {
				
				Message message = (Message) Utils.fromJSON((String) res, Message.class);
				String string = Utils.getMessageContent(message);
				if (string == null || string.equals("")) {
					Toast.makeText(MaterialDetailActivity.this, getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
				} else {
					ResultInfo resultInfo = (ResultInfo)Utils.fromJSON(string, ResultInfo.class);
					int result = resultInfo.getResult();
					if (result == 8210) {
						Toast.makeText(MaterialDetailActivity.this, resultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(MaterialDetailActivity.this, resultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
					}
				}
			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			CollectionSDInfo request = (CollectionSDInfo)params[0];
			String json = Utils.toJSON(request);
			Message message = new Message(8200, true, json);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
		
	}
}
